package com.hbsx.why.base;

import android.app.Application;
import android.content.Context;

import com.hbsx.why.utils.SpKeyUtils;
import com.hbsx.why.utils.cache.SPUtil;
import com.hbsx.why.utils.changeskin.SkinManager;
import com.jock.cityselecter.db.DBManager;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

/**
 * Created by liub on 2017/6/26 13:52.
 */

public class BaseApplication extends Application {
    private static BaseApplication instance;
    private SPUtil spUtil;
    private DBManager dbHelper;
    //内存泄漏
    private RefWatcher refWatcher;

    @Override
    public void onCreate() {
        super.onCreate();
        dbHelper = new DBManager(this);
        dbHelper.openDatabase();
        instance = this;
        spUtil = new SPUtil(this);
        //SkinManager.getInstance().init(instance);
        ZXingLibrary.initDisplayOpinion(this);
        //内存泄漏
        refWatcher = setupLeakCanary();
    }

    private RefWatcher setupLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return RefWatcher.DISABLED;
        }
        return LeakCanary.install(this);
    }

    public static RefWatcher getRefWatcher(Context context) {
        BaseApplication leakApplication = (BaseApplication) context.getApplicationContext();
        return leakApplication.refWatcher;
    }

    public SPUtil getSpUtil() {
        if (spUtil == null)
            spUtil = new SPUtil(this);
        return spUtil;
    }

    public boolean isFirstEnter() {
        return getSpUtil().getBoolean(SpKeyUtils.FIRST_ENTER, true);
    }

    public void setNotFirstEnter() {
        getSpUtil().putBoolean(SpKeyUtils.FIRST_ENTER, false);
    }

    public static BaseApplication getInstance() {
        return instance;
    }
}
